import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/cons/global_cons.dart';
import 'package:flutter_shop/pages/home/home_router.dart';
import 'package:flutter_shop/pages/home/home_tab_page/entity/home_hot_data.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/routers/navigator_utils.dart';
import 'package:flutter_shop/widget/load_image.dart';

///火爆专区商品
class HotGoods extends StatelessWidget {
  final List<HomeHotData> list;

  HotGoods({Key key, this.list}) : super(key: key);

  ///火爆标题
  Widget _hotTitle() {
    return Container(
      padding: Paddings.common,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            width: 20,
            height: 20,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: CommonColors.primary,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Text(
              "火",
              style: TextStyle(color: Colors.white, fontSize: CommonDimens.font_size_small),
            ),
          ),
          Text(
            "火爆专区",
            style: TextStyles.common_text,
          )
        ],
      ),
    );
  }

  ///火爆商品
  Widget _hotGoods() {
    return Container(
      child: GridView.builder(
          itemCount: list.length,
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
          ),
          itemBuilder: _buildItem),
    );
  }

  ///商品item
  Widget _buildItem(context, index) {
    HomeHotData item = list[index];

    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            left: BorderSide(width: 0.5, color: CommonColors.common_divide_line),
            top: BorderSide(width: 0.5, color: CommonColors.common_divide_line),
            right: BorderSide(width: 0.5, color: CommonColors.common_divide_line),
            bottom: BorderSide(width: 0.5, color: CommonColors.common_divide_line),
          )),
      child: RawMaterialButton(
        onPressed: () {
          NavigatorUtils.push(context, HomeRouter.goodDetail, params: {GlobalCons.KEY_ID: item.goodsId});
        },
        child: Column(
          children: <Widget>[
            Expanded(child: LoadImage(item.image)),
            Container(
              padding: EdgeInsets.fromLTRB(5, 5, 5, 10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(item.name, style: TextStyles.primary_text, maxLines: 1, overflow: TextOverflow.ellipsis),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(
                        "¥${item.mallPrice}",
                        style: TextStyles.common_text,
                      ),
                      Text(
                        "¥${item.price}",
                        style: TextStyle(
                          fontSize: CommonDimens.font_size_small,
                          color: CommonColors.gray,
                          decoration: TextDecoration.lineThrough,
                        ),
                      ),
                    ],
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          _hotTitle(),
          _hotGoods(),
        ],
      ),
    );
  }
}
